package playGround.square;
import exceptions.OverflowException;
import be.kuleuven.cs.som.annotate.*;

/**
 * An enumeration of temperaturescales.
 * The possible scales are: Kelvin, Fahrenheit and Celcius.
 *  
 * @author Niels Claeys and Maarten Christiaen
 *
 */
@Value
public enum Scale {
	FAHRENHEIT('F'), CELCIUS('C'),KELVIN('K') ;
	
	@Raw
	private Scale(char unit){
		this.unit = unit;
	}
	private final char unit;
	
	@Raw @Immutable @Basic
	public char getUnit(){
		return this.unit;
	}
	/**
	 * 
	 * @param 	oldTemperatureScale
	 * 			The old temperature scale.
	 * @param 	newTemperatureScale
	 * 			The new temperature scale.
	 * @param 	temperature
	 * 			The given temperature whose scale is about to get changed.
	 * @return	The temperature, expressed in the new scale.
	 * 			| if (oldTemperatureScale== Scale.CELCIUS)
	 * 			|		if(newTemperatureScale == Scale.FAHRENHEIT)
	 * 			|			result == ((temperature *9/5)+32)
	 * 			|		if(newTemperatureScale==Scale.KELVIN) 
	 * 			|			result ==  temperature+273
	 * 			|		if(newTemperatureScale == Scale.CELCIUS)
	 * 			|			result == temperature
	 * 			| if(oldTemperatureScale == Scale.KELVIN)
	 * 			|		if(newTemperatureScale == Scale.FAHRENHEIT)
	 * 			|			result == (((temperature-273)*9/5)+32)
	 * 			|		if(newTemperatureScale==Scale.KELVIN) 
	 * 			|			result ==  temperature
	 * 			|		if(newTemperatureScale == Scale.CELCIUS)
	 * 			|			result == temperature-273
	 * 			| if(oldTemperatureScale == Scale.FAHRENHEIT)
	 * 			|		if(newTemperatureScale == Scale.FAHRENHEIT)
	 * 			|			result == temperature
	 * 			|		if(newTemperatureScale==Scale.KELVIN) 
	 * 			|			result == ((temperature -32)*5/9)+273
	 * 			|		if(newTemperatureScale == Scale.CELCIUS)
	 * 			|			result == ((temperature -32)*5/9)
	 * @throws 	OverflowException
	 * 			The sum of the two values that are about to get added is larger than the max value of an integer.
	 * 			| result > Ingeger.MAX_VALUE
	 */
	public static int changeScale(Scale oldTemperatureScale, Scale newTemperatureScale, int temperature)throws OverflowException{
		if(oldTemperatureScale== Scale.CELCIUS){
			if(newTemperatureScale == Scale.FAHRENHEIT){
				if(temperature>((Integer.MAX_VALUE)*5/9))
					throw new OverflowException(temperature, 4/5*temperature );
				if(temperature>((Integer.MAX_VALUE-32)*5/9))
					throw new OverflowException(temperature*9/5, 32);
				return temperature*9/5+32;
			}
			if(newTemperatureScale==Scale.KELVIN) {
				if(temperature>(Integer.MAX_VALUE-273))
					throw new OverflowException(temperature, 273);
				return temperature+273;
			}
			if(newTemperatureScale == Scale.CELCIUS)
				return temperature;
		}
		else if(oldTemperatureScale == Scale.FAHRENHEIT){
			return changeScale(Scale.CELCIUS,newTemperatureScale,(temperature -32)*5/9);
		}
		
		else if(oldTemperatureScale == Scale.KELVIN){
			return changeScale(Scale.CELCIUS,newTemperatureScale,temperature-273);
		}
		return temperature;
		
	}


}
